{% extends "base.html" %}
{% load i18n %}
{% block head %}
    <title>{% trans "发送通知" %}</title>
    {{ block.super }}
    <link href="https://magicbox.bk.tencent.com/static_api/v3/bk/css-pro/bk.css" rel="stylesheet">
    <link href="https://magicbox.bk.tencent.com/static_api/v3/assets/bk-icon-2.0/iconfont.css" rel="stylesheet">
{% endblock %}

{% block content %}

    <div style="width:1200px; margin: 0 auto">
        <form class="bk-form bk-form-vertical" style="width:600px;">
            <div class="bk-form-item">
                <label class="bk-label">邮箱地址</label>
                <div class="bk-form-content">
                    <input type="text" id="email" class="bk-form-input" name="validation_name" placeholder="请输入">
                </div>
            </div>
            <div class="bk-form-item">
                <label class="bk-label">邮件标题</label>
                <div class="bk-form-content">
                    <input type="text" id="title" class="bk-form-input ignore" placeholder="请输入">
                </div>
            </div>
            <div class="bk-form-item">
                <label class="bk-label">邮件内容</label>
                <div class="bk-form-content">
                    <textarea name="" id="content" class="bk-form-textarea ignore" placeholder="请输入"></textarea>
                </div>
            </div>
            <div class="bk-form-item bk-form-action">
                <div class="bk-form-content">
                    <a class="bk-button bk-primary" id="submit" title="保存">保存</a>
                    <a class="bk-button" title="取消" disabled="true">取消</a>
                </div>
            </div>
        </form>
    </div>
    <script>

    </script>
{% endblock %}
{% block extra_block %}
    <script src="https://magicbox.bk.tencent.com/static_api/v3/assets/bkMessage-1.0/js/bkMessage.js"></script>
    <script src="https://magicbox.bk.tencent.com/static_api/v3/assets/bkDialog-2.0/js/bkDialog.js"></script>
    <script>
        const csrftoken = "{{ csrf_token }}";
        $.ajaxSetup({
            beforeSend: function (xhr, settings) {

                function csrfSafeMethod(method) {
                    // these HTTP methods do not require CSRF protection
                    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
                }

                if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                    xhr.setRequestHeader('X-CSRFToken', csrftoken);
                }
            },
        });
        function loading() {
            var dialog = new bkDialog({
                type: 'loading',
                closeIcon: false,
            });
            return dialog
        }

        function tips(msg, theme) {
            new bkMessage({
                message: msg,
                theme: theme
            });
        }

        $(function () {
            $("#submit").click(res => {
                let email = $("#email").val();
                let title = $("#title").val();
                let content = $("#content").val();
                console.log(email, title, content);
                if (!(email && title && content)) {
                    tips("缺少参数,请检查", "error");
                    return
                }
                loading().show();
                $.ajax({
                    url: site_url + 'notice/',
                    method: 'post',
                    dataType: "json",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(
                        {
                            title: title,
                            email: email,
                            content: content
                        }
                    ),

                    success: function (res) {
                        loading().close();
                        console.log(res.data);
                        if (res.result) {
                            tips("发送成功", "success")
                        } else {
                            tips(res.message, "error")
                        }

                    },
                    error: function () {
                        tips("系统异常,请联系管理员", "error")
                    }
                })

            })
        })
    </script>
{% endblock %}